package db

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego"
	_"github.com/go-sql-driver/mysql"
	"github.com/xgggh/test/api/entity"
	"fmt"
)

var master string="default"
var mMaster orm.Ormer
func init() {
	user := beego.AppConfig.String("mysql.user")
	pass := beego.AppConfig.String("mysql.pass")
	host := beego.AppConfig.String("mysql.host")
	ports,_:= beego.AppConfig.Int64("mysql.port")
	db := beego.AppConfig.String("mysql.db")
	debug,_:=beego.AppConfig.Bool("mysql.debug")
	args := fmt.Sprintf(
		"%s:%s@tcp(%s:%d)/%s?charset=utf8&loc=UTC",
		user,
		pass,
		host,
		ports,
		db,
	)
	// set default database
	maxIdle := 30
	maxConn := 30 //
	orm.RegisterDataBase(master, "mysql",args ,maxIdle, maxConn)

	// register model
	orm.RegisterModel(new(entity.Task),
		new (entity.Param),
		new(entity.Result),
	)
	// create table
	orm.RunSyncdb(master, false, true)

	orm.Debug=debug
}

func Master() orm.Ormer{
	if mMaster==nil {
		mMaster=orm.NewOrm();
		mMaster.Using(master)
	}
	return mMaster;
}


